import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/components/home_list.dart';
import 'package:flutter_enjoy_plus/components/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';
import 'package:flutter_enjoy_plus/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  //? 获取社区公告列表
  List annoucementList = [];
  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  //? 获取公告列表
  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    setState(() {
      annoucementList = res;
    });
    print(res);
  }

  //? 封装的List Widget -> 操作token
  Widget getListToken() {
    return Row(
      children: [
        Expanded(
          child: ElevatedButton(
            // onPressed: () async {
            //   final token = await TokenManager().getToken();
            //   PromptAction.showSuccess('获取到的token是: $token');
            // },
            // child: const Text('获取token'),
            onPressed: () {
              TokenManager().deleteToken();
            },
            child: const Text('删除token'),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              // TokenManager().deleteToken();
              // PromptAction.showSuccess('删除token成功');
              timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                _count_down--;
                setState(() {});
              });
            },
            child: const Text('开启定时器'),
          ),
        ),
        Text('剩余：$_count_down秒'),
        Expanded(
            child: ElevatedButton(
          onPressed: () {
            timer?.cancel();
          },
          child: const Text('取消定时器'),
        )),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              Future.delayed(const Duration(seconds: 3), () {
                PromptAction.showSuccess('定时器');
              });
            },
            child: const Text('延时器 '),
          ),
        )
      ],
    );
  }

  Timer? timer;
  int _count_down = 60;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          ElevatedButton(
              onPressed: () {
                // showDatePicker(
                //     context: context,
                //     firstDate: DateTime.now(),
                //     lastDate: DateTime.now().add(const Duration(days: 365)));
                // showAboutDialog(context: context, children: [
                //   const Text('享+社区'),
                //   const Text('享+社区'),
                //   const Text('享+社区'),
                //   const Text('享+社区'),
                //   const Text('享+社区'),
                // ]);

                // showBottomSheet(
                //     context: context,
                //     builder: (context) {
                //       return const SizedBox(
                //         height: double.infinity,
                //         width: double.infinity,
                //         child: Text('测试'),
                //       );
                //     });
                // showDialog(
                //     context: context,
                //     builder: (context) {
                //       return const AlertDialog(
                //         title: Text('测试'),
                //       );
                //     });
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(
                    content: Text('点击了：'),
                    duration: Duration(seconds: 1),
                  ),
                );
              },
              child: const Text('点吧')),
          //? 上nav
          getListToken(),
          HomeNav(),
          //? 公告图片
          ClipRRect(
            borderRadius: BorderRadiusDirectional.circular(10),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),
          //? 列表数据
          HomeList(notifyList: annoucementList)
        ],
      ),
    );
  }
}
